<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="姓名" prop="leaderName">
        <el-input
          v-model="queryParams.leaderName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter="handleQuery"
          style="width: 100px"
        />
      </el-form-item>
      <el-form-item label="学号" prop="leaderId">
        <el-input
          v-model="queryParams.leaderId"
          placeholder="请输入学号"
          clearable
          @keyup.enter="handleQuery"
          style="width: 100px"
        />
      </el-form-item>
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="参赛项目"
          clearable
          @keyup.enter="handleQuery"
           style="width: 100px"
        />
      </el-form-item>

      <el-form-item label="赛道名称" prop="racewayName">
        <el-select
          v-model="queryParams.racewayName"
          placeholder="请选择赛道"
          clearable
          @change="handleQuery"
          style="width: 100px"
        >
          <el-option
            v-for="item in trackList"
            :key="item.teamContestId"
            :label="item.trackName"
            :value="item.trackName"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="审核状态" prop="auditFlag" style="width: 180px">
        <el-select
          v-model="queryParams.auditFlag"
          placeholder="请选择审核状态"
          clearable
          @change="handleQuery"
        >
          <el-option label="审核中" value="0" />
          <el-option label="通过" value="1" />
          <el-option label="未通过" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:teamregistration:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:teamregistration:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:teamregistration:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:teamregistration:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="teamregistrationList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="参赛项目名称" align="center" prop="projectName" />
      <el-table-column label="负责人姓名" align="center" prop="leaderName" />
      <el-table-column label="负责人学号" align="center" prop="leaderId" />
      <el-table-column label="指导老师" align="center" prop="teamTeacher" />

      <el-table-column label="赛道名称" align="center" prop="racewayName" />
      <!--   <el-table-column label="参赛项目描述" align="center" prop="projectDes" /> -->
      <!--  <el-table-column label="是否完成官网报名(0:未报名,1:已报名)" align="center" prop="applyStatus" /> -->
      <!--    <el-table-column label="团队人数" align="center" prop="teamNumber" /> -->
      <!--    <el-table-column label="关于比赛的想法和规划" align="center" prop="competitionPlan" /> -->
      <!--  <el-table-column label="招募需求描述" align="center" prop="recruitmentDesc" /> -->
      <el-table-column label="审核状态" align="center">
        <template #default="scope">
          <span v-if="scope.row.auditFlag === '0'">审核中</span>
          <span v-else-if="scope.row.auditFlag === '1'">通过</span>
          <span v-else>未通过</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:teamregistration:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:teamregistration:remove']"
            >删除</el-button
          >
            <el-button
            link
            type="success"
            icon="Operation"
            @click="handleMember(scope.row)"
            >参赛成员</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改团队赛队伍报名对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="teamregistrationRef" :model="form" :rules="rules" label-width="80px">
        <!--  <el-form-item label="团队赛id" prop="competitionId">
          <el-input v-model="form.competitionId" placeholder="团队赛id" />
        </el-form-item> -->
        
        <el-form-item label="竞赛名称" prop="competitionName">
          <el-input
            v-model="form.competitionName"
            placeholder="请输入竞赛具体名称"
            disabled
          />
        </el-form-item>

        <el-form-item label="赛道名称" prop="racewayName">
          <el-select
            v-model="form.racewayName"
            placeholder="请选择赛道"
            @change="handleTrackChange"
          >
            <el-option
              v-for="item in trackList"
              :key="item.teamContestId"
              :label="item.trackName"
              :value="item.trackName"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="form.projectName" placeholder="请输入参赛项目名称" />
        </el-form-item>
        <el-form-item label="项目描述" prop="projectDes">
          <el-input
            v-model="form.projectDes"
            type="textarea"
            placeholder="请输入参赛项目描述"
          /> </el-form-item
        ><el-form-item label="想法规划" prop="competitionPlan">
          <el-input
            v-model="form.competitionPlan"
            type="textarea"
            placeholder="关于比赛的想法和规划"
          />
        </el-form-item>
        <el-form-item label="负责人" prop="leaderName">
          <el-input v-model="form.leaderName" placeholder="请输入负责人姓名" />
        </el-form-item>
        <el-form-item label="学号" prop="leaderId">
          <el-input v-model="form.leaderId" placeholder="请输入负责人学号" />
        </el-form-item>
        <el-form-item label="指导老师" prop="teamTeacher">
          <el-input v-model="form.teamTeacher" placeholder="请输入指导老师" />
        </el-form-item>

        <!--  <el-form-item label="团队人数" prop="teamNumber">
          <el-input v-model="form.teamNumber" placeholder="请输入团队人数" />
        </el-form-item> -->
        <el-form-item label="审核状态" prop="auditFlag">
          <el-select v-model="form.auditFlag" placeholder="请选择审核状态">
            <el-option label="待审核" value="0" />
            <el-option label="通过" value="1" />
            <el-option label="未通过" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="招募需求" prop="recruitmentDesc">
          <el-input
            v-model="form.recruitmentDesc"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志(0:存在,2:删除)" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Teamregistration">
import {
  listTeamregistration,
  getTeamregistration,
  delTeamregistration,
  addTeamregistration,
  updateTeamregistration,
} from "@/api/competitionmanagement/teamregistration";
import { listTeamrecord } from "@/api/competitionmanagement/teamrecord";
import { useRouter } from 'vue-router'
import {onMounted} from 'vue'


// 定义props
const props = defineProps({
  competitionName: {
    type: String,
    default: "",
  },
  competitionId: {
    type: Number,
    default: null,
  },


});
const { proxy } = getCurrentInstance();
const router = useRouter()

const teamregistrationList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const trackList = ref([]); // 赛道列表

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    competitionId:null,
    teamLeader: null,
    leaderId: null,
    leaderName: null,
    leaderClass: null,
    teamTeacher: null,
    projectName: null,
    projectDes: null,
    applyStatus: null,
    teamNumber: null,
    competitionPlan: null,
    recruitmentDesc: null,
    auditFlag: null,
    racewayName: null,
    competitionName: props.competitionName,
  },
});

const { queryParams, form, rules } = toRefs(data);

// 获取赛道列表
function getTrackList() {
  listTeamrecord({ competitionId: props.competitionId }).then(response => {

    const tracks = response.rows;
    // 去重处理
    const uniqueTracks = [];
    const trackNames = new Set();
    tracks.forEach(item => {
      if (!trackNames.has(item.trackName)) {
        trackNames.add(item.trackName);
        uniqueTracks.push(item);
      }
    });
    trackList.value = uniqueTracks;
  });
}

// 处理赛道选择变化
function handleTrackChange(val) {
  if (val) {
    const selectedTrack = trackList.value.find(item => item.trackName === val);
    if (selectedTrack) {
      form.value.competitionId = selectedTrack.id;
    }
  }
}

/** 查询团队赛队伍报名列表 */
function getList() {
  loading.value = true;
  listTeamregistration(queryParams.value).then((response) => {
    teamregistrationList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    competitionId: null,
    teamLeader: null,
    leaderId: null,
    leaderName: null,
    leaderClass: null,
    teamTeacher: null,
    projectName: null,
    projectDes: null,
    applyStatus: null,
    teamNumber: null,
    competitionPlan: null,
    recruitmentDesc: null,
    createBy: null,
    createTime: null,
    delFlag: null,
    auditFlag: null,
    competitionName: props.competitionName,
    racewayName: null,
  };
  proxy.resetForm("teamregistrationRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getTrackList();
getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  getTrackList();
  open.value = true;
  title.value = "添加团队赛队伍报名";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  getTrackList();
  const _id = row.id || ids.value;
  getTeamregistration(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改团队赛队伍报名";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["teamregistrationRef"].validate((valid) => {
    if (valid) {
      proxy.$modal.loading("正在提交中...");
      if (form.value.id != null) {
        updateTeamregistration(form.value).then((response) => {
          proxy.$modal.closeLoading();
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        }).catch(() => { proxy.$modal.closeLoading(); });
      } else {
        addTeamregistration(form.value).then((response) => {
          proxy.$modal.closeLoading();
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        }).catch(() => { proxy.$modal.closeLoading(); });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除团队赛队伍报名编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delTeamregistration(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "system/teamregistration/export",
    {
      ...queryParams.value,
    },
    `teamregistration_${new Date().getTime()}.xlsx`
  );
}

/** 参赛成员按钮操作 */
function handleMember(row) {
  router.push({
    path: "/competitionmanagement/teammember",
    query: {
      id: row.id,
    },
  });
}
onMounted(() => {
  getList();
  getTrackList();
})
</script>
